package com.example.fw;

import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class HelperWithWebDriverBase extends HelpersBase {

	protected WebDriver driver;
	protected String baseUrl;
	
	public static String[] MONTHS = new String[] { "January", "February",
		"March", "April", "May", "June", "July", "August", "September",
		"October", "November", "December" };

	public Random rnd = new Random();

	public String month = MONTHS[new Random()
			.nextInt(generateRandomNumberBetween(1, 11))];
	public String day = Integer.toString(generateRandomNumberBetween(1, 28));
	public String year = Integer.toString(generateRandomNumberBetween(1900,
			2013));

	public HelperWithWebDriverBase(ApplicationManager manager) {
		super(manager);
		driver = manager.getWebDriverHelper().getDriver();
		baseUrl = manager.getProperty("baseUrl");
	}

	protected void type(String locator, String name) {
		driver.findElement(By.name(locator)).clear();
		driver.findElement(By.name(locator)).sendKeys(name);
	}

	protected boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	public WebElement findElement(By locator) {
		return driver.findElement(locator);
	}

	public void getURL(String url) {
		driver.get(url);
	}

	protected void click(By locator) {
		driver.findElement(locator).click();
	}

	protected void selectValue(String locator, String bDay) {
		new Select(findElement(By.name(locator))).selectByVisibleText(bDay);
	}

	protected List<WebElement> findElements(By locator) {
		return driver.findElements(locator);
	}

	protected void selectRandomElement(String locator) {
		Random group = new Random();
		List<WebElement> groupList = findElements(By.name(locator));
		if (groupList.size() > 0) {
			groupList.get(group.nextInt(groupList.size())).click();
		}
	}

	protected void selectRandomElements(int n, String locator) {
		for (int i = 0; i < n; i++) {
			selectRandomElement(locator);
		}
	}

	public int generateRandomNumberBetween(int start, int end) {
		return new Random().nextInt(end - start) + start;
	}

	protected void selectElement(String locator, int i) {
		if (!findElements(By.name(locator)).get(i).isSelected()) {
			findElements(By.name(locator)).get(i).click();
		}
	}

	protected void selectRandom(By locator) {
		Select select = new Select(driver.findElement(locator));
		List<WebElement> options = select.getOptions();
		if (options.size() > 0) {
			int index = new Random().nextInt(options.size());
			options.get(index).click();
		}
	}

	protected String stripString(String title) {
		Pattern p = Pattern.compile(".*\\((.*)\\)");
		Matcher m = p.matcher(title);
		if (m.matches()) {
			title = m.group(1);
		}
		return title;
	}

}
